from abc import ABC, abstractmethod
from collections import defaultdict
from surprise import Dataset, Reader


class BaseDatasetLoader(ABC):
    """通用数据集加载器抽象基类"""

    id_to_name = defaultdict(str)
    name_to_id = defaultdict(int)

    @abstractmethod
    def load_dataset(self) -> Dataset:
        """加载并返回Surprise格式的数据集"""
        pass

    @abstractmethod
    def get_user_ratings(self, user_id: int) -> list:
        """获取指定用户的评分记录"""
        pass

    @abstractmethod
    def get_item_features(self) -> defaultdict:
        """获取物品特征数据"""
        pass

    @abstractmethod
    def get_popularity_ranks(self) -> defaultdict:
        """获取物品流行度排名"""
        pass

    @abstractmethod
    def get_popularity_itemIDs(self):
        """获取最流行的物品列表 [(itemID, rating)...]"""
        pass

    # 公共方法
    def get_name(self, item_id: int) -> str:
        return self.id_to_name.get(item_id, "")

    def get_id(self, item_name: str) -> int:
        return self.name_to_id.get(item_name, 0)
